﻿using GPS.MODELS;
using GPS.DataAccessLayer;
using GPS.COMMON;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using PagedList;
using System.Data.SqlClient;
using System;
using System.Data.Entity.Validation;
using System.Net;

using CaptchaMvc;
using CaptchaMvc.HtmlHelpers;
using GPS.Models.AccountManagement;
using System.Reflection;
using System.Text;
using System.IO;
using AutoMapper;


namespace GPS.Controllers.GSP_Tracking
{
    public class GPS_TrackingLoginController : Controller
    {
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }

        //
        // GET: /GPS_TrackingLogin/
        public ActionResult Index(ViewModelAccountLog SearchModel)
        {
            ApplicationDbContext _db = new ApplicationDbContext();
            SearchModel.MA_TTP = this.getCurrentUser().MA_TINHTP;
            initialCategorySearchAction(SearchModel);

            var entities = _db.AccountLogs.Where(o => ((SearchModel.MA_TTP == null || SearchModel.MA_TTP == "40") || o.MA_TTP == SearchModel.MA_TTP)
                                                             && (SearchModel.Username == null || o.Username.Contains(SearchModel.Username))
                                                             && (SearchModel.Fullname == null || o.Fullname == SearchModel.Fullname)
                                                             && (SearchModel.Logtime_From == null || o.Logtime >= SearchModel.Logtime_From)
                                                             && (SearchModel.Logtime_To == null || o.Logtime <= SearchModel.Logtime_To)
                                                             );

            var model = new List<AccountLog>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<AccountLog>(currentEntity));
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }

        public void initialCategorySearchAction(ViewModelAccountLog viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();
            DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MA == viewModel.MA_TTP), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;


        }

    }
}